Ext.namespace("org.microsite.server");

org.microsite.server.ServerEditor = Ext.extend(Ext.Window, {
    title : 'Edit/create server',
    closable : true,
    modal : true,
    width : 500,
    height : 400,
    layout : 'fit',
    iconCls : 'servericon',
    
    data : null,
    parentObject : null,
    success : false,

    cbType : null,
    txtName : null,
    txtVersion : null,
    txtDescription : null,
    txtPath : null,
    txtDataPath : null,
    txtService : null,
    txtPort : null,
    cbAdmin : null,
    
    frm : null,

    initComponent : function() {
        this.cbType = new org.microsite.util.ObjectSelector({
            service : ServerRemote.listTypes,
            reader : new Ext.data.StringReader({id: 'name'}, Ext.data.Record.create([{name: 'name'}])),
            selectedValue : this.data ? this.data.type : null,

            valueField : 'name',
            hiddenName : 'type',
            fieldLabel : 'Server type',
            anchor : '100%'
        });
	    this.txtName = new Ext.form.TextField({
            name : 'name',
            fieldLabel : 'Server name',
            anchor : '100%',
            allowBlank : false,
            value : this.data ? this.data.name : '',
            emptyText : 'This field is required'
        });
        this.txtVersion = new org.microsite.util.AutoField({
            service : ServerRemote,
            columnName : 'version',
            
            name : 'version',
            fieldLabel : 'version',
            value : this.data ? this.data.version : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtDescription = new Ext.form.TextArea({
            name : 'description',
            fieldLabel : 'Description',
            value : this.data ? this.data.description : '',
            anchor : '100%'
        })
        this.txtPath = new org.microsite.util.AutoField({
            service : ServerRemote,
            columnName : 'path',
            
            name : 'path',
            fieldLabel : 'Path',
            value : this.data ? this.data.path : '',
            anchor : '100%'
        });
        this.txtDataPath = new org.microsite.util.AutoField({
            service : ServerRemote,
            columnName : 'dataPath',
            
            name : 'dataPath',
            fieldLabel : 'Additional path',
            value : this.data ? this.data.dataPath : '',
            anchor : '100%'
        });
        this.txtService = new org.microsite.util.AutoField({
            service : ServerRemote,
            columnName : 'service',
            
            name : 'service',
            fieldLabel : 'Service',
            value : this.data ? this.data.dataPath : '',
            anchor : '100%'
        });
        this.txtPort = new Ext.form.TextField({
            name : 'port',
            fieldLabel : 'Port',
            anchor : '100%',
            value : this.data ? this.data.port : ''
        });
        this.cbAdmin = new org.microsite.util.ObjectSelector({
            type : User,
            selectedValue : this.data ? this.data.admin : null,

            hiddenName : 'admin',
            fieldLabel : 'Server admin',
            anchor : '100%'
        });
        this.frm = new Ext.FormPanel({
            bodyStyle : 'padding: 5px',
            items : [this.cbType, this.txtName, this.txtVersion, this.txtDescription, this.txtPath,
                     this.txtDataPath, this.txtService, this.txtPort, this.cbAdmin]
        });

        this.btnOK = new Ext.Button({
            text : 'OK',
            handler : this.onOKClicked,
            scope : this
        });
        this.btnCancel = new Ext.Button({
            text : 'Cancel',
            handler : this.onCancelClicked,
            scope : this
        });
        

        Ext.apply(this, {
            items : [this.frm],
            buttons : [this.btnOK, this.btnCancel]
        });

        org.microsite.server.ServerEditor.superclass.initComponent.apply(this);

        this.addEvents({
            'change' : true
        });
    },
    
    onOKClicked : function(btn, e) {
        var server = Ext.apply(new Server(), this.frm.getForm().getValues(), this.data);
        if (this.parentObject)  server.host = this.parentObject.id;
        this.success = true;
        this.fireEvent('change', server);
        this.close();
    },
    
    onCancelClicked : function(btn, e) {
        this.close();
    }
});